import Mock from 'mockjs' // 导入 mockjs
import { useSysClassStore } from "../stores/class.ts";
import { storeToRefs } from 'pinia';

const useSysClass = useSysClassStore()


// 新增班级
Mock.mock("/api/save/new/class", 'post', (options) => {
    const classs = JSON.parse(options.body);
    console.log('Mock 添加的班级:', classs)
    const newClass = {
        teaerId: classs.teacherId,
        teacherName: classs.teacherName,
        className: classs.className,
        studentId: classs.studentId,
        studentName: classs.studentName,
    };
    const addedClass = useSysClass.addClass(newClass);
    console.log("新添加的班级:", addedClass);
    return {
        code: 200,
        msg: '添加成功',
        data: null,
    }
})

// 获取所有班级信息
Mock.mock("/api/allClassData", 'get', (options) => {
    const addClassData = useSysClass.getAllClasses
    console.log('Mock 获取的 classes:', addClassData)
    const list = []
    addClassData.forEach((item) => {
        list.push({
            id: item.id,
            name: item.className,
        })
    })
    return {
        code: 200,
        msg: '查询成功',
        data: list,
    }
})

// 修改班级信息
Mock.mock("/api/update/class", 'post', (options) => {
    // 1. 解析前端发送过来的数据
    const updatedClassData = JSON.parse(options.body);

    // 2. 从“数据库”中找到原始的班级信息
    const originalClass = useSysClass.getClassById(updatedClassData.id);

    if (!originalClass) {
        // 如果找不到班级，返回错误
        return {
            success: false,
            message: '班级不存在'
        };
    }

    // 3. --- 核心逻辑：合并 studentId 数组 ---
    // 确保前端传来的 studentId 是一个数组，防止出错
    const newStudentIds = Array.isArray(updatedClassData.studentId) ? updatedClassData.studentId : [];
    const newStudentName =Array.isArray(updatedClassData.studentName) ? updatedClassData.studentName : [];

    // 使用 Set 来轻松合并并去重
    const mergedStudentIds = [...new Set([...originalClass.studentId, ...newStudentIds])];
    const mergedStudentName = [...new Set([...originalClass.studentName, ...newStudentName])];

    const newObject = {
        id: updatedClassData.id,
        studentId: mergedStudentIds,
        studentName: mergedStudentName,
    }

    console.log('Mock 修改的班级:', newObject)
    const is = useSysClass.updateClass(newObject);
    if ( is){
        return {
            code: 200,
            msg: '修改成功',
            data: "修改成功",
        }
    }else {
        return {
            code: 500,
            msg: '修改失败',
            data: null,
        }
    }
});


// 条件查询
Mock.mock("/api/query/class", 'post', (options) => {
    const obj = JSON.parse(options.body);
    try {
        const { findPapersByConditions  } = storeToRefs(useSysClass);
        // 调用 getter，它会返回一个函数
        const findFunction = findPapersByConditions .value;
        // 执行这个函数并传入查询条件
        const filteredPapers = findFunction(obj);
        console.log('Mock 获取的查询结果:', filteredPapers)
        return {
            code: 200,
            msg: '查询成功',
            data: filteredPapers,
        };
    }catch (e) {
        return {
            code: 500,
            msg: '查询失败',
            data: e,
        };
    }
})